﻿/**
 * 
 */
package com.game.jumpdino;

//import com.google.ads.AdSenseSpec;
//import com.google.ads.GoogleAdView;
//import com.google.ads.AdSenseSpec.AdType;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ComponentName;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;

import com.adwhirl.AdWhirlLayout;
import com.adwhirl.AdWhirlManager;
import com.adwhirl.AdWhirlTargeting;
import com.adwhirl.adapters.AdWhirlAdapter;
import com.skt.arm.aidl.IArmService;

/**
 * @author HugoGu
 *
 */
public class MenuPage extends Activity {
	Intent intent;
	Animation clickAnimation;
	
 	// add for SKT
	private IArmService 						service;
	private ArmServiceConnection 				armCon;
	private String 								resMsg;
	private String								AID = 				"OA00043346";


//	// Replace with your own AdSense client ID.
//    private static final String CLIENT_ID = "ca-mb-app-pub-0512298971600735";
//
//    // Replace with your own company name.
//    private static final String COMPANY_NAME = "TT mobile Tech, Inc";
//
//    // Replace with your own application name.
//    private static final String APP_NAME = "Jumping Panda";
//
//    // Replace with your own keywords used to target Google ad.
//    // Join multiple words in a phrase with '+' and join multiple phrases with ','.
//    private static final String KEYWORDS = "Jumping Panda,Disney,Kids,accident,lawyer";
//
//    // Replace with your own AdSense channel ID.
//    private static final String CHANNEL_ID = "2408206872";
//    private GoogleAdView adView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        /*
        if(!runService()){
          	resMsg = "ARM PROBLEM"; 
      		showDialog(0);
        }
        */
        setContentView(R.layout.menu);
        
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout_main);
        AdWhirlLayout  adWhirlLayout = new AdWhirlLayout(this, "62e3ac7c054c42bd8f027393fb22c789");
        final int DIP_WIDTH = 320;
        final int DIP_HEIGHT = 72;
        final float DENSITY = getResources().getDisplayMetrics().density;
        int scaledWidth = (int) (DENSITY * DIP_WIDTH + 0.5f);
        int scaledHeight = (int) (DENSITY * DIP_HEIGHT + 0.5f);
        RelativeLayout.LayoutParams adWhirlLayoutParams =
        new RelativeLayout.LayoutParams(scaledWidth, scaledHeight);
        layout.addView(adWhirlLayout, adWhirlLayoutParams);
        layout.invalidate();
        
        AdWhirlAdapter.setGoogleAdSenseAppName("Pocket Swiss Rail Clock");
        AdWhirlAdapter.setGoogleAdSenseCompanyName("TTmbile");
        AdWhirlAdapter.setGoogleAdSenseExpandDirection("BOTTOM");
        AdWhirlAdapter.setGoogleAdSenseChannel("2408206872");
        AdWhirlManager.setConfigExpireTimeout(1000 * 60 * 5);
        AdWhirlTargeting.setAge(23);
        AdWhirlTargeting.setGender(AdWhirlTargeting.Gender.MALE);
        AdWhirlTargeting.setKeywords("pocket swiss rail clock, application, tools, productivity, time, watch, clock, train, transportation, travel, local, lifestyle");
        AdWhirlTargeting.setPostalCode("94123");
        AdWhirlTargeting.setTestMode(false);
        
        
        intent = new Intent();
        intent.setClass(MenuPage.this, JumpingPanda.class);
        clickAnimation = AnimationUtils.loadAnimation(this, R.anim.click);
    	clickAnimation.reset();
//    	 GoogleAdView adView = (GoogleAdView) findViewById(R.id.adview);
//         AdSenseSpec adSenseSpec =
//            new AdSenseSpec(CLIENT_ID)     // Specify client ID. (Required)
//             .setCompanyName(COMPANY_NAME)  // Set company name. (Required)
//             .setAppName(APP_NAME)          // Set application name. (Required)
//             .setKeywords(KEYWORDS)         // Specify keywords.
//             .setChannel(CHANNEL_ID)        // Set channel ID.
//             .setAdType(AdType.TEXT)        // Set ad type to Text.
//             .setAdTestEnabled(false);       // Keep true while testing.
//         adView.showAds(adSenseSpec);
//        Typeface font = Typeface.createFromAsset(getAssets(), "jokerman.ttf");
//        if (font != null){
//        	TextView view = (TextView) this.findViewById(R.id.start_btn);
//        	if (view != null) {
//        		view.setTypeface(font);  
//        	}
//        	view = (TextView) this.findViewById(R.id.exit_btn);
//        	if (view != null) {
//        		view.setTypeface(font);
//        	}
//        	view = (TextView) this.findViewById(R.id.about_btn);
//        	if (view != null) {
//        		view.setTypeface(font);
//        	}
//        }
    }
    
    public void onExitClick(View target){
    	runAnimations(target, new AnimationListener()
    	{
			public void onAnimationEnd(Animation animation) {
		        finish();
			}

			public void onAnimationRepeat(Animation animation) {				
			}

			public void onAnimationStart(Animation animation) {				
			}    		
    	});
    }
    
    public void onAboutClick(final View target) {
    	runAnimations(target, new AnimationListener()
    	{
			public void onAnimationEnd(Animation animation) {
				target.setVisibility(4);
		        finish();
		        Intent intent = new Intent();
		        intent.setClass(MenuPage.this, AboutPage.class);
		        startActivity(intent);
			}

			public void onAnimationRepeat(Animation animation) {				
			}

			public void onAnimationStart(Animation animation) {				
			}
		});
   	}
    
    public void onStartClick(final View target) {
    	runAnimations(target, new AnimationListener()
    	{
			public void onAnimationEnd(Animation animation) {
				target.setVisibility(4);
		        finish();
		        startActivity(intent);
			}

			public void onAnimationRepeat(Animation animation) {				
			}

			public void onAnimationStart(Animation animation) {				
			}
		});
   	}
    
    private void runAnimations(View tv, AnimationListener listener) {
    	clickAnimation.setAnimationListener(listener);
	    tv.clearAnimation();
	    tv.startAnimation(clickAnimation);
	}
	class ArmServiceConnection implements ServiceConnection {
		public void onServiceConnected(ComponentName name, IBinder boundService) {
			if (service == null)
				service = IArmService.Stub.asInterface((IBinder) boundService);
			try {
				int res = service.executeArm(AID);
				resMsg = "" + errorString(res);
				switch (res) {
				case 1:
					// 闈规爣杞�闉侊拷
					// Application 闉濇洢鍎�鐢戭剢褰�
					break;
				default:
					showDialog(0);
					break;
				}
			} catch (Exception e) {
				e.printStackTrace();
				releaseService();
				resMsg = "error";
				showDialog(0);
				finish();
				return;
			}
			releaseService();
		}

		public void onServiceDisconnected(ComponentName name) {
			service = null;
		}
	}

	private boolean runService() {
		try {
			if (armCon == null) {
				armCon = new ArmServiceConnection();
				boolean conRes = bindService(new Intent(IArmService.class
						.getName()), armCon, Context.BIND_AUTO_CREATE);
				if (conRes)
					return true;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		releaseService();
		return false;
	}

	private void releaseService() {
		if (armCon != null) {
			unbindService(armCon);
			armCon = null;
			service = null;
		}
	}
	private String errorString(int res) {
		String sErrorString = new String();
		switch (res) {
		case 0xF0000004:
			sErrorString = new String(
					" 闉氭瀚撻灊渚呮緫 闉涖儸鏆婃惪锟介湜妫�緤闈圭娋濮�姘氭粔绗侀瀽锟介瀬銈嶅皭闋冩﹥姒鹃瀫鎯﹀闆硷拷闉涚娋瀚撻爥鍕虫 闆笺倢瀚�闉佹粖寮伴爟锟芥妫�闉佹粚娈�(04) ");
			break;
		case 0xF0000008:
			sErrorString = new String(
					" 闉氭瀚撻灊渚呮緫 闉涖儸鏆婃惪锟介湜妫�緤闈圭娋濮�姘氭粔绗侀瀽锟介瀬銈嶅皭闋冩﹥姒鹃瀫鎯﹀闆硷拷闉涚娋瀚撻爥鍕虫 闆笺倢瀚�闉佹粖寮伴爟锟芥妫�闉佹粚娈�(08) ");
			break;
		case 0xF000000E:
			sErrorString = new String(
					" 闉氭瀚撻灊渚呮緫 闉涖儸鏆婃惪锟介湜妫�緤闈圭娋濮�姘氭粔绗侀瀽锟介瀬銈嶅皭闋冩﹥姒鹃瀫鎯﹀闆硷拷闉涚娋瀚撻爥鍕虫 闆笺倢瀚�闉佹粖寮伴爟锟芥妫�闉佹粚娈�(0E) ");
			break;
		case 0xF0000009:
			sErrorString = new String(
					" 闈镐締鎷�鐢戭剢袚闆尗妫�闋囨洢婢戦瀽锟介瀬銈嶅皭闋冩﹥姒鹃瀫鎯﹀闆硷拷闉涙劤鍔爟锟介澐顒堟殯闉氾拷鐡跨娂鐨鹃澒鏌姍鎼匡拷姘嶈兂娼�姘氭棅鐎洪浖鍫у珶.(09) 	");
			break;
		case 0xF000000A:
			sErrorString = new String(
					" Tstore 姘欐垐鐨滈灈鍛烘偝 闆碱煄顬忛灈鍛哄闆硷拷 鑷э拷鐎�闋夛拷闉氭尗姣勯灇锟介爟鎸紲闉佹粔璧�姘氭棅鐎洪浖鍫у珶.(0A) ");
			break;
		case 0xF000000C:
			sErrorString = new String(
					" 闉氭瀚撻灊渚呮緫 闉涖儸鏆婃惪锟介湜妫�緤闈圭娋濮�姘氭粔绗侀瀽锟介瀬銈嶅皭闋冩﹥姒鹃瀫鎯﹀闆硷拷闉涚娋瀚撻爥鍕虫 闆笺倢瀚�闉佹粖寮伴爟锟芥妫�闉佹粚娈�(0C ) ");
			break;
		case 0xF000000D:
			sErrorString = new String(
					" 闉忛敊鏀叉顒囩磻闉氭尗鍘撮灇锟介湜妫�緤闈圭娋濮�闉濇洡鐐�闋囨洢婢戦灇锟芥數鍫︾殰闆汇儹鏆曢浖鍫у珶.闉涙劤鍔爟锟介澐顒堟殯闉氾拷鐡跨娂鐨鹃澒鏌姍鎼匡拷姘嶈兂娼�姘氭棅鐎洪浖鍫у珶.(0D) ");
			break;
		case 0xF0000011:
			sErrorString = new String(
					" 闋冮附鎽愰熃锟芥癌鍫╂▋姣筹拷闋囨洢婢戦爟锟介澗锟介瀽鍡㈠Х闆煎牕瀚� USIM 闉涖儸鐧岄瀽顒嗙К 闋囨洢婢�姘氾拷USIM 闉涚娂绗�灇锟介渽锟界摯灞婃瘞 闋冩尗鐗呮锟介爟鎸紲闉佹粔璧�姘氭棅鐎洪浖鍫у珶.(11) ");
			break;
		case 0xF0000012:
			sErrorString = new String(
					" 闉忛敊鏀叉顒囩磻闉氭尗鍘撮灇锟介灊鏇ょ倰 闋囨洢婢戦灇锟芥數鍫︾殰闆汇儹鏆曢浖鍫у珶.闉涙劤鍔爟锟介澐顒堟殯闉氾拷鐡跨娂鐨鹃澒鏌姍鎼匡拷姘嶈兂娼�姘氭棅鐎洪浖鍫у珶.(12) ");
			break;
		case 0xF0000013:
			sErrorString = new String(
					" 闋冮附鎽愰熃鍔炴闈癸拷闆垮姙婢婇煯锟藉枌闉侊拷3G, WIFI)闉氾拷闇呮﹣纰�闉涘牗顫�闉庡濮烽浖鍫у珶.闋冮附鎽愰熃鍔炴剑 闆垮姙婢婇煰锟介煴濂犲珰 闈广倢鐖兼數锟界С闉氾拷闋囨洢婢�闋夛拷闉涳拷闉併倣鏋嗛灇锟介爟锟芥妫�闉佹粚娈�(13) ");
			break;
		case 0xF0000014:
			sErrorString = new String(
					" Tstore 闉濆嫵姣勯爞鍕差敗鏀撮附鐏楅灇锟介澒銈岀 闇呮﹥鏌�闉涘牗顫�闉庡濮烽浖鍫у珶.Tstore 闉濆嫵姣勯爞鍕差敗鏀撮附鐏楅灇锟介澒銈岀闋冩﹥瀚�闋夛拷闉涳拷闉併倣鏋嗛灇锟介爟锟芥妫�闉佹粚娈�(14) ");
			break;
		default:
			sErrorString = new String(" UNKNOW ERROR");
		}
		return sErrorString;
	}

	protected Dialog onCreateDialog(int id) {
		return new AlertDialog.Builder(this).setOnCancelListener(new DialogInterface.OnCancelListener() {
			   public void onCancel(DialogInterface arg0) {
				    System.exit(0);
				   }
				  })//鐎电鐦藉锟介崣鏍ㄧХ閺冨墎娈戞禍瀣╂

		.setIcon(
				R.drawable.alert_dialog_icon).setTitle("Error").setMessage(
				resMsg).setPositiveButton("OK",
				new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int whichButton) {
						finish();
					}
				}).create();
	}
    
}
